package com.raising.utils;

import com.raising.framework.shiro.entity.CurrentUser;
import com.raising.modules.sys.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * 用户管理工具
 * 
 * @author gaoy
 * 
 */
public class UserUtils {


	/**
	 * 获取当前登录用户
	 * @return
	 */
	public static User getLoginUser() {
		try{
			Subject subject = SecurityUtils.getSubject();
			CurrentUser currentUser = (CurrentUser) subject.getPrincipal();
			User user = new User();
			user.setId(currentUser.getId());
			user.setUsername(currentUser.getUsername());
			user.setOrgId(currentUser.getOrgId());
			user.setOrgNm(currentUser.getOrgNm());
			user.setOrgNo(currentUser.getOrgNo());
			user.setRoleIdsStr(currentUser.getRoleIds());
			return user;
		}catch (Exception e){
			return null;
		}
	}

	/**
	 * 获取当前登录CurrentUser对象
	 * @author GaoYuan
	 * @date 2018/12/12 下午5:01
	 */
	private static CurrentUser getCurrentUser() {
		Subject subject = SecurityUtils.getSubject();
		return (CurrentUser) subject.getPrincipal();
	}


	/**
	 * 获取当前访问用户名
	 * @return
	 */
	public static String getUserName() {
		return getCurrentUser().getUsername();
	}

	/**
	 * 获取当前用户标识
	 * @return
	 */
	public static String getUserId() {
		return getCurrentUser().getId();
	}

	/**
	 * 获取当前组织机构标识
	 * @return
	 */
	public static String getOrgId() {
		return getCurrentUser().getOrgId();
	}

	/**
	 * 获取当前组织机构编号
	 * @return
	 */
	public static String getOrgNo() {
		return getCurrentUser().getOrgNo();
	}


	/**
	 * 退出当前用户
	 * @author GaoYuan
	 * @datetime 2018/1/13 上午8:50
	 */
	public static void logout(){
		try{
			SecurityUtils.getSubject().logout();
		}catch (Exception e){
			//这里故意捕获不处理
			e.printStackTrace();
		}
	}

}
